import { defineStore } from "pinia";
import type { UserInfo } from "../types";

interface UserState {
    userInfo: UserInfo | null;
    token: string;
    isLogin: boolean;
}

export const useUserStore = defineStore("user", {
    state: (): UserState => ({
        userInfo: uni.getStorageSync("userInfo") || null,
        token: uni.getStorageSync("token") || "",
        isLogin: !!uni.getStorageSync("token")
    }),

    getters: {
        /**
         * 获取用户ID
         */
        userId: (state): string | number => {
            return state.userInfo?.id || "";
        },

        /**
         * 获取用户名
         */
        username: (state): string => {
            return state.userInfo?.username || "";
        },

        /**
         * 获取用户昵称
         */
        nickname: (state): string => {
            return state.userInfo?.nickname || "";
        },

        /**
         * 获取用户头像
         */
        avatar: (state): string => {
            return state.userInfo?.avatar || "";
        }
    },

    actions: {
        /**
         * 设置 Token
         */
        setToken(token: string) {
            this.token = token;
            this.isLogin = !!token;
            uni.setStorageSync("token", token);
        },

        /**
         * 设置用户信息
         */
        setUserInfo(userInfo: UserInfo) {
            this.userInfo = userInfo;
            this.isLogin = true;
            uni.setStorageSync("userInfo", userInfo);
        },

        /**
         * 登录
         */
        async login(loginData: { username: string; password: string }) {
            try {
                // TODO: 这里调用登录 API
                // const res = await loginApi(loginData)

                // 模拟登录响应
                const mockUser: UserInfo = {
                    id: 1,
                    username: loginData.username,
                    nickname: "测试用户",
                    avatar: ""
                };
                const mockToken = "mock-token-123456";

                this.setUserInfo(mockUser);
                this.setToken(mockToken);

                return Promise.resolve(mockUser);
            } catch (error) {
                return Promise.reject(error);
            }
        },

        /**
         * 退出登录
         */
        logout() {
            this.userInfo = null;
            this.token = "";
            this.isLogin = false;

            uni.removeStorageSync("token");
            uni.removeStorageSync("userInfo");

            // 跳转到登录页
            uni.reLaunch({
                url: "/pages/login/login"
            });
        },

        /**
         * 更新用户信息
         */
        updateUserInfo(userInfo: Partial<UserInfo>) {
            if (this.userInfo) {
                this.userInfo = { ...this.userInfo, ...userInfo };
                uni.setStorageSync("userInfo", this.userInfo);
            }
        },

        /**
         * 从本地存储恢复用户信息
         */
        restoreUserInfo() {
            const userInfo = uni.getStorageSync("userInfo");
            const token = uni.getStorageSync("token");

            if (userInfo && token) {
                this.userInfo = userInfo;
                this.token = token;
                this.isLogin = true;
            }
        }
    }
});
